<?php

/*
fungsi dropdown umum yg mengambil data dari database
dataTableDropdown('nama_tabel','name_field_inputnya','option_valuenya','nama_tampil_field','terpilih=id_nya')
*/

function dataTableDropdown($table=FALSE,$var_name=FALSE,$val_id=FALSE,$field_name=FALSE,$selected=FALSE)
{
	$ci=& get_instance();
	$row = $ci->db->get($table)->result_array();
	$nama = ''.$field_name.'';
	$id = ''.$val_id.'';
    	$output = "<select class='' name='".$var_name."' style='width:200px;' id='kab_id'>";
		    foreach($row as $row):
		    	if($selected):
		    		if($row[$id]==$selected):
						$output .= "<option value='".$row[$id]."' selected='selected'>".$row[$nama]."</option>";
					else:
						$output .= "<option value='".$row[$id]."'>".$row[$nama]."</option>";
					endif;
		    	else:
		    		$output .= "<option value='".$row[$id]."'>".$row[$nama]."</option>";
		    	endif;
		    endforeach;
	    $output .= "</select>";
    return $output;
}


function provDropdown($selected=FALSE)
{
	$ci=& get_instance();
	$ci->db->select('id_provinsi,nama_provinsi');
	$ci->db->order_by('id_provinsi');
	$row = $ci->db->get('tb_provinsi')->result_array();
	
    	$output = "<select class='validate(required)' id='id_provinsi' style='width:200px;' name='id_provinsi' onChange='showKab(this.value)'>";
			$output .= "<option  value=''> - Pilih - </option>";
		    foreach($row as $row):
		    	if($selected):
		    		if($row["id_provinsi"]==$selected):
						$output .= "<option value='".$row["id_provinsi"]."' selected='selected'>".$row['nama_provinsi']."</option>";
					else:
						$output .= "<option value='".$row["id_provinsi"]."'>".$row['nama_provinsi']."</option>";
					endif;
		    	else:
		    		$output .= "<option value='".$row["id_provinsi"]."'>".$row['nama_provinsi']."</option>";
		    	endif;
		    endforeach;
	    $output .= "</select>";
    return $output;
}


function jenkelDropdown($selected){
	$class = 'class="validate(required)" id="jenkel" style="width:100px;"';
	$options = array(
				  '' => '- Pilih -',
                  'Laki - laki'=> 'Laki - laki',
                  'Perempuan' => 'Perempuan'
                );
	return form_dropdown('jenkel',$options,$selected,$class);
}

/*
Fungsi tanggal secara dropdown
dateDropdown('nama_inputnya','terpilih=field_date')
*/

function dateDropdown($name,$date=FALSE)
{
	$ci=& get_instance();
	if($date){
	$dt = explode('-',$date);
	$year = $dt[0];
	$mon = $dt[1];
	$day = $dt[2];
	}
	
	$ret = '<select class="sel_input" name="'.$name.'_d"  style="width:50px;">';
	for ($i=1;$i<32;$i++):
		$val=$i;
		if($val < 10):
			$val = '0'.$val;
		endif;
		if ($date):
			if($val == $day):
				$ret .= '<option value="'.$val.'" selected="selected">'.$i.'</option>';
			else:
				$ret .= '<option value="'.$val.'">'.$i.'</option>';
			endif;
		else:
			$ret .= '<option value="'.$val.'">'.$i.'</option>';
		endif;
	endfor;
	$ret .='</select> ';

	$month = array	('Jan','Feb','Mar','Apr','Mei','Jun','Jul','Ags','Sep','Okt','Nov','Des');
	$ret .= '<select class="sel_input" name="'.$name.'_m"  style="width:60px;">';
	for ($i=0;$i<12;$i++):
		$val=$i+1;
		if($val < 10):
			$val = '0'.$val;
		endif;
		if ($date):
			if($val == $mon):
				$ret .= '<option value="'.$val.'" selected="selected">'.$month[$i].'</option>';
			else:
				$ret .= '<option value="'.$val.'">'.$month[$i].'</option>';
			endif;
		else:
			$ret .= '<option value="'.$val.'">'.$month[$i].'</option>';
		endif;
	endfor;
	$ret .='</select> ';

	$minyear = 1998;
	$plusyear = 2002;

	$ret .= '<select class="sel_input" name="'.$name.'_y" style="width:58px;">';
	for ($i=$minyear;$i<=$plusyear;$i++):
		if ($date):
			if($i==$year):
				$ret .= '<option value="'.$i.'" selected="selected">'.$i.'</option>';
			else:
				$ret .= '<option value="'.$i.'">'.$i.'</option>';
			endif;
		else:
			$ret .= '<option value="'.$i.'">'.$i.'</option>';
		endif;
	endfor;
	$ret .='</select>';
return $ret;

}

/*
Fungsi convert tanggal ke tanggalan indonesia
*/
function indDate($date) 
{
	$arr_bulan = array("Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "Nopember", "Desember");
	$arr_date = explode("-", $date);
	$arr_date[1] = intval($arr_date[1]);
	$arr_date[2] = intval($arr_date[2]);
	return $arr_date[2].' '.$arr_bulan[$arr_date[1] - 1].' '.$arr_date[0];
}

/*
Fungsi convert tanggal ke tanggalan english
*/

function enDate($date) 
{
	$arr_bulan = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	$arr_date = explode("-", $date);
	$arr_date[1] = intval($arr_date[1]);
	$arr_date[2] = intval($arr_date[2]);
	
	if ($arr_date[2] == 1)
	{
		$day = $arr_date[2].'st';
	}
	elseif ($arr_date[2] == 2)
	{
		$day = $arr_date[2].'nd';
	}
	elseif ($arr_date[2] == 3)
	{
		$day = $arr_date[2].'rd';
	}
	else
	{
		$day = $arr_date[2].'th';
	}
	
	return $arr_bulan[$arr_date[1] - 1].' '.$arr_date[0].', '.$day;
}


/**
* function loadTinyMCE
* @uses loadTinyMCE($theme) $theme = advance, simple
* @filesource application/helpers/ies_helper.php
*/
function load_tiny_mce($textarea_ID)
{
	$output = '<script type="text/javascript" src="'.base_url().'assets/js/jquery_tiny_mce/jquery.tinymce.js"></script>';
	$output .= '
	<script type="text/javascript">
	$(function() {
	    $("textarea.tinymce").tinymce({
	        // Location of TinyMCE script
	        script_url: "'.base_url().'assets/js/jquery_tiny_mce/tiny_mce.js",

	        // General options
	        mode: "specific_textareas",
	        editor_selector: "mceEditor",
	        relative_urls: false,
	        remove_script_host: false,
	        document_base_url: "'.base_url().'",
	        theme: "advanced",
	        skin: "cirkuit",
	        plugins: "pdw,safari,pagebreak,style,table,advimage,advlink,emotions,inlinepopups,preview,media,contextmenu,paste,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,wordcount",
	        // Theme options
	        theme_advanced_buttons1: "bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,blockquote,bullist,numlist,formatselect,kitchensink",
	        theme_advanced_buttons2: "pastetext,pasteword,forecolor,charmap,sub,sup,outdent,indent,undo,redo,link,unlink,anchor,preview,fullscreen,help",
	        theme_advanced_buttons3: "tablecontrols,hr,media,nonbreaking,pagebreak",
	        theme_advanced_toolbar_location: "top",
	        theme_advanced_toolbar_align: "left",
	        theme_advanced_statusbar_location: "bottom",
	        theme_advanced_resizing: true,
	        theme_advanced_resize_horizontal: false,
	        pdw_toggle_on: true,
	        pdw_toggle_toolbars: "2,3",

	        // Content CSS
	        content_css: "'. base_url().'assets/js/jquery_tiny_mce/css/content.css",

	        setup: function(ed) {
	            // Add a custom button
	            ed.addButton("kitchensink", {
	                title: "Show toolbar",
	                image: "'. base_url().'assets/images/famfamfam/cog.png",
	                onclick: function() {
	                    var thisEditor = tinyMCE.getInstanceById("'.$textarea_ID.'");
	                    thisEditor.execCommand("mcePDWToggleToolbars");
	                }
	            });
	        }
	    });
	});
	</script>
	';
	echo $output;
}

function makeslug($string){
	$slug= strtolower(preg_replace('/[^A-Za-z0-9-]+/', '-', $string));
	$slug = str_replace(" ", "-", $slug);
	return $slug;
}

function makeName($string){
	$slug= strtolower(preg_replace('/[^A-Za-z0-9-]+/', '_', $string));
	$slug = str_replace(" ", "_", $slug);
	return $slug;
}


/**
* function formatDate
* @uses mmemformat tanggal
* @filesource application/helpers/tb_helper.php
*/
function formatDate($date)
{
	$dt = explode('-',$date);
	$y = $dt[0];
	$m = $dt[1];
	$d = $dt[2];
	
	$newDate = $d.'-'.$m.'-'.$y;
	
	return $newDate;

}

/*fungsi untuk upload file*/
function inf_upload($file)
{
	$ci =& get_instance();

	$config['upload_path'] = './uploads/file/';
	$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|docx|ppt|pptx|odt|mp3|mp4|ogg|wav|mp4|m4v|mov|wmv|avi|mpg|ogv|3gp|3g2|gz';
	$config['max_size']	= '4000';
	$config['max_width']  = '2048';
	$config['max_height']  = '2048';

	$ci->load->library('upload', $config);

	if ( ! $ci->upload->do_upload($file)):
		$return_data = array('status' => 'error','error' =>$ci->upload->display_errors('<div class="error">', '</div>'));
		return $return_data;
	else:
		$return_data = array('status' => 'success','data' =>$ci->upload->data());
		return $return_data;
	endif;
}

/*fungsi untuk resize gambar*/

function inf_resize($image,$width,$height,$filename,$ext,$filepath)
{
	$ci =& get_instance();

	$config['image_library'] = 'gd2';
	$config['source_image'] = $image;
	$config['new_image'] = $filename.'_'.$width.'x'.$height.$ext;
	$config['maintain_ratio'] = TRUE;
	$config['height'] = $height;
	$config['width'] = $width;

	$ci->image_lib->initialize($config);
	$ci->image_lib->resize();

	$image_size = getimagesize($filepath.$config['new_image']);
	$image_width = $image_size['0'];
	$image_height = $image_size['1'];
	rename($filepath.$config['new_image'],$filepath.$filename.'_'.$image_width.'x'.$image_height.$ext);
	return $filename.'_'.$image_width.'x'.$image_height.$ext;
	$ci->image_lib->clear();
}

/*fungsi untuk croping gambar*/

function inf_crop_box($image,$size,$filename,$ext,$filepath)
{
	$ci =& get_instance();

	$image_size = getimagesize($image);
	$image_width = $image_size['0'];
	$image_height = $image_size['1'];

	$config['image_library'] = 'gd2';
	$config['source_image'] = $image;
	$config['new_image'] = $filename.'_'.$size.'x'.$size.$ext;
	$config['maintain_ratio'] = FALSE;
	if($image_width > $image_height):
		$config['height'] = $image_height;
		$config['width'] = $image_height;
	elseif($image_width < $image_height):
		$config['height'] = $image_width;
		$config['width'] = $image_width;
	else:
		$config['height'] = $image_width;
		$config['width'] = $image_width;
	endif;

	if($image_width > $image_height):
		$config['x_axis'] = (10/100)*$image_width;
		$config['y_axis'] = '0';
	elseif($image_width < $image_height):
		$config['x_axis'] = '0';
		$config['y_axis'] = (10/100)*$image_height;
	else:
		$config['x_axis'] = (10/100)*$image_width;
		$config['y_axis'] = '0';
	endif;
	$ci->image_lib->initialize($config);
	$ci->image_lib->crop();

	$ci->image_lib->clear();

	$config2['image_library'] = 'gd2';
	$config2['source_image'] = $filepath.$config['new_image'];
	$config2['maintain_ratio'] = TRUE;
	$config2['height'] = $size;
	$config2['width'] = $size;

	$ci->image_lib->initialize($config2);
	$ci->image_lib->resize();

	return $config['new_image'];

}



function media_imageurl_by_mimetype($media_mime_type,$media_ID,$filename=FALSE)
{
	//masih banyak mime type yang harus ditambahkan
	$ci=& get_instance();
	if ($media_mime_type == 'application/pdf'
		OR $media_mime_type == 'application/x-download'
		OR $media_mime_type == 'application/powerpoint'
		OR $media_mime_type == 'application/vnd.ms-powerpoint'):
		return base_url().'assets/images/crystal/document.png';
	elseif ($media_mime_type == 'application/excel'
			OR $media_mime_type == 'application/vnd.ms-excel'
			OR $media_mime_type == 'application/msexcel'
			OR $media_mime_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'):
		return base_url().'assets/images/crystal/spreadsheet.png';
	elseif ($media_mime_type == 'audio/mpeg'
		OR $media_mime_type == 'audio/mpg'
		OR $media_mime_type == 'audio/midi'
		OR $media_mime_type == 'audio/x-aiff'
		OR $media_mime_type == 'audio/x-pn-realaudio'
		OR $media_mime_type == 'audio/x-pn-realaudio-plugin'
		OR $media_mime_type == 'audio/x-realaudio'):
	return base_url().'assets/images/crystal/audio.png';
	elseif ($media_mime_type == 'application/x-director'
		OR $media_mime_type == 'application/x-dvi'
		OR $media_mime_type == 'application/x-gtar'
		OR $media_mime_type == 'application/x-gzip'
		OR $media_mime_type == 'application/x-httpd-php'
		OR $media_mime_type == 'application/x-httpd-php-source'
		OR $media_mime_type == 'application/x-javascript'
		OR $media_mime_type == 'application/x-shockwave-flash'
		OR $media_mime_type == 'application/x-stuffit'
		OR $media_mime_type == 'application/x-tar'):
	return base_url().'assets/images/crystal/archive.png';
	elseif ($media_mime_type == 'image/jpeg'
			OR $media_mime_type == 'image/pjpeg'
			OR $media_mime_type == 'image/png'
			OR $media_mime_type == 'image/gif'
			OR $media_mime_type == 'image/bmp'
			OR $media_mime_type == 'image/x-png'
			OR $media_mime_type == 'image/tiff'):
			//if(media_metadata($media_ID,'thumbnail')):
				//return base_url().'/public/media/'.media_metadata($media_ID,'thumbnail');
			//else:
			//	return base_url().'/public/media/'.$filename;
			//endif;

		//return base_url().'assets/images/crystal/spreadsheet.png';
	else:
		return base_url().'assets/images/crystal/document.png';
	endif;
}



